<!-- eslint-disable space-before-function-paren -->
<template>
  <div id="app">
    <!--    路由匹配到的组件将渲染在这里-->
    <router-view v-if="isReload" class="card-box"></router-view>
  </div>
</template>

<script>
import Main from './views/Main'
import Home from './layout/Home'
import HospitalApply from './layout/admin/hospitalAdmin/HospitalApply'
// import HospitalManage from './layout/admin/hospitalAdmin/HospitalManage'
import Department from './layout/hospital/Department'
import DoctorManager from './layout/hospital/DoctorManager'
import Book from './layout/user/Book'
import BookDetail from './layout/user/BookDetail'
import DoctorBook from './layout/doctor/DoctorBook'
import DoctorIntroduce from './layout/doctor/DoctorIntroduce'
import HospitalHomeEdit from './layout/hospital/HospitalHomeEdit'
import FrontDeskOrder from './layout/frontDesk/frontDeskOrder'
import MedicalIntroduce from './layout/admin/medical/MedicalIntroduce'
import MedicalIntroduceAdd from './layout/admin/medical/MedicalIntrodeAdd'
import MedicalManage from './layout/medicine/MedicalManage'
import MedicalManageHistory from './layout/medicine/MedicalManageHistory'
import MedicalSell from './layout/user/MedicalSell'
import ShoppingCart from './layout/user/ShoppingCart'
import MedicalOrder from './layout/user/MedicalOrder'
import AddressManage from './layout/user/AddressManage'
import PlaceOrder from './layout/user/PlaceOrder'
import OrderManage from './layout/medicine/OrderManage'
import Booking from './layout/doctor/Booking'
import BookRecord from './layout/doctor/BookRecord'
import Prescription from './layout/medicine/Prescription'
import Utils from './api/utils'
import NotFound from './layout/NotFound'
export default {
  name: 'App',
  created () {
    const permissions = JSON.parse(localStorage.getItem('userInfo') || '[]')
    this.addRoutes(permissions)
  },
  provide () {
    return {
      reload: this.reload
    }
  },
  data () {
    return {
      isReload: true
    }
  },
  methods: {
    addRoutes (permissions) {
      const dynamicRoutes = generateDynamicRoutes(permissions.type)
      dynamicRoutes.map(item => {
        this.$router.addRoute(item)
      })
    },
    reload () {
      this.isReload = false
      this.$nextTick(() => {
        this.isReload = true
      })
    }
  },
  mounted () {
    var that = this
    Utils.$on('addRoutes', function (permissions) {
      that.addRoutes(permissions)
    })
  }
}
function generateDynamicRoutes (permissions) {
  const dynamicRoutes = []
  // 根据权限信息动态生成路由配置
  if (permissions === 'admin') {
    dynamicRoutes.push(
      {
        path: '/',
        component: Main,
        children: [
          { path: 'home', component: Home, name: 'home' }, // all permission,
          {
            path: 'medical-introduce',
            component: MedicalIntroduce,
            name: 'medical-introduce',
            meta: {
              requiresAuth: true
            }
          }, // admin
          {
            path: 'medical-introduce-add',
            component: MedicalIntroduceAdd,
            name: 'medical-introduce-add',
            meta: {
              requiresAuth: true
            }
          }, // admin
          {
            path: 'hospital-apply',
            component: HospitalApply,
            name: 'hospitalApply',
            meta: {
              requiresAuth: true
            }
          } // admin
          // {
          //   path: 'hospital-manage',
          //   component: HospitalManage,
          //   name: 'hospital-manage',
          //   meta: {
          //     requiresAuth: true
          //   }
          // }
        ]
      },
      {
        path: '/404',
        component: NotFound
      },
      {
        path: '*',
        redirect: '/404'
      }
      // admin
    )
  } else if (permissions === ('user')) {
    dynamicRoutes.push(
      {
        path: '/',
        component: Main,
        children: [
          { path: 'home', component: Home, name: 'home' }, // all permission,
          {
            path: 'book',
            component: Book,
            name: 'book',
            meta: {
              requiresAuth: true
            }
          },
          {
            path: 'book-detail',
            component: BookDetail,
            name: 'book-detail',
            meta: {
              requiresAuth: true
            }
          }, // user
          {
            path: 'medical-sell',
            component: MedicalSell,
            name: 'medical-sell',
            meta: {
              requiresAuth: true
            }
          }, // user
          {
            path: 'shopping-cart',
            component: ShoppingCart,
            name: 'shopping-cart',
            meta: {
              requiresAuth: true
            }
          }, // user
          {
            path: 'medical-order',
            component: MedicalOrder,
            name: 'medical-order',
            meta: {
              requiresAuth: true
            }
          }, // user
          {
            path: 'address-manage',
            component: AddressManage,
            name: 'address-manage',
            meta: {
              requiresAuth: true
            }
          }, // user
          {
            path: 'place-order',
            component: PlaceOrder,
            name: 'place-order',
            meta: {
              requiresAuth: true
            }
          }
        ]
      },
      {
        path: '/404',
        component: NotFound
      },
      {
        path: '*',
        redirect: '/404'
      })
  } else if (permissions === ('medicine')) {
    dynamicRoutes.push(
      {
        path: '/',
        component: Main,
        children: [
          { path: 'home', component: Home, name: 'home' }, // all permission,
          {
            path: 'medical-manage',
            component: MedicalManage,
            name: 'medical-manage',
            meta: {
              requiresAuth: true
            }
          }, // medicine
          {
            path: 'prescription',
            component: Prescription,
            name: 'prescription',
            meta: {
              requiresAuth: true
            }
          }, // medicine
          {
            path: 'medical-manage-history',
            component: MedicalManageHistory,
            name: 'medical-manage-history',
            meta: {
              requiresAuth: true
            }
          }, // medicine
          {
            path: 'order-manage',
            component: OrderManage,
            name: 'order-manage',
            meta: {
              requiresAuth: true
            }
          }// medicine
        ]
      },
      {
        path: '/404',
        component: NotFound
      },
      {
        path: '*',
        redirect: '/404'
      })
  } else if (permissions === ('doctor')) {
    dynamicRoutes.push(
      {
        path: '/',
        component: Main,
        children: [
          { path: 'home', component: Home, name: 'home' }, // all permission,
          {
            path: 'doctor-book',
            component: DoctorBook,
            name: 'doctor-book',
            meta: {
              requiresAuth: true
            }
          }, // doctor
          {
            path: 'doctor-introduce',
            component: DoctorIntroduce,
            name: 'doctor-introduce',
            meta: {
              requiresAuth: true
            }
          }, // doctor
          {
            path: 'booking',
            component: Booking,
            name: 'booking',
            meta: {
              requiresAuth: true
            }
          }, // doctor
          {
            path: 'book-record',
            component: BookRecord,
            name: 'book-record',
            meta: {
              requiresAuth: true
            }
          } // doctor
        ]
      },
      {
        path: '/404',
        component: NotFound
      },
      {
        path: '*',
        redirect: '/404'
      })
  } else if (permissions === ('hospital')) {
    dynamicRoutes.push(
      {
        path: '/',
        component: Main,
        children: [
          {path: 'home', component: Home, name: 'home'},
          {
            path: 'department',
            component: Department,
            name: 'department',
            meta: {
              requiresAuth: true
            }
          }, // hospital
          {
            path: 'doctor-manager',
            component: DoctorManager,
            name: 'doctor-manager',
            meta: {
              requiresAuth: true
            }
          }, // hospital
          {
            path: 'doctor-home-edit',
            component: HospitalHomeEdit,
            name: 'doctor-home-edit',
            meta: {
              requiresAuth: true
            }
          } // hospital
        ]
      },
      {
        path: '/404',
        component: NotFound
      },
      {
        path: '*',
        redirect: '/404'
      }
    )
  } else if (permissions === ('frontDesk')) {
    dynamicRoutes.push(
      {
        path: '/',
        component: Main,
        children: [
          {path: 'home', component: Home, name: 'home'},
          {
            path: 'front-desk-book',
            component: FrontDeskOrder,
            name: 'front-desk-book',
            meta: {
              requiresAuth: true
            }
          }
        ]
      },
      {
        path: '/404',
        component: NotFound
      },
      {
        path: '*',
        redirect: '/404'
      }
    ) // frontDesk
  } else {
    dynamicRoutes.push(
      {
        path: '/',
        component: Main,
        children: [
          { path: 'home', component: Home, name: 'home' } // all permission,
        ]
      },
      {
        path: '/404',
        component: NotFound
      },
      {
        path: '*',
        redirect: '/404'
      })
  }
  return dynamicRoutes
}
</script>

<style lang="less">
html,
body {
  margin: 0;
  padding: 0;
}

// el-table去掉滚动条
.el-table__body-wrapper::-webkit-scrollbar {
  width: 0;
}

//修改el-card的边距
.el-card__body,
.el-main {
  padding: 10px;
}
</style>
